package com.ate.aoligei;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.ate.aoligei.db.DbHelper;
import com.ate.aoligei.db.DbManager;

import java.util.List;

public class MainActivity2 extends AppCompatActivity implements View.OnClickListener {

    private Button add;
    private Button delete;
    private Button query;
    private EditText id;
    private Button update;
    private EditText name;
    private EditText mark;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();
    }

    private void initView() {
        add = (Button) findViewById(R.id.add);
        delete = (Button) findViewById(R.id.delete);
        query = (Button) findViewById(R.id.query);
        id = (EditText) findViewById(R.id.id);
        update = (Button) findViewById(R.id.update);

        add.setOnClickListener(this);
        delete.setOnClickListener(this);
        query.setOnClickListener(this);
        update.setOnClickListener(this);
        name = (EditText) findViewById(R.id.name);
        name.setOnClickListener(this);
        mark = (EditText) findViewById(R.id.mark);
        mark.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add:
                Student student = new Student();
                student.setName(name.getText().toString().trim());
                student.setMark(mark.getText().toString().trim());
                DbHelper.insertStudent(student);
                break;
            case R.id.delete:
                DbHelper.delete();
                break;
            case R.id.query:
                List<Student> students = DbHelper.queryAll();
                for (Student student1 : students) {
                    Log.e("name="+student1.getName(),"mark="+student1.getMark());
                }
                break;
            case R.id.update:
                String trim = id.getText().toString().trim();
                DbHelper.update(new Student(Long.parseLong(trim),"asd","asd"));
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        DbManager.getInstance().closeDataBase();
    }
}